package org.example.dyscript;

import com.google.common.base.Preconditions;
import org.example.dyscript.dynamicscript.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.HashMap;
import java.util.Map;

@Warmup(iterations = 5)
@Measurement(iterations = 5)
@BenchmarkMode({Mode.Throughput, Mode.AverageTime})
@Fork(2)
public class DynamicScriptRuleBenchmark {

  static Rule emptyRule = new EmptyRule();
  static Rule javaRule = new JavaRule();
  static Rule jexlRule = new JexlRule();
  static Rule mvelRule = new MvelRule();
  static Rule spELRule = new SpELRule();
  static Rule qLExpressRule = new QLExpressRule();
  static Rule groovyScriptRule = new GroovyScriptRule();
  static Rule aviatorRule = new AviatorRule();
  static Rule ognlRule = new OGNLRule();
  static Rule felRule = new FelRule();
  static Rule jselRule = new JselRule();
  static Rule groovyClassRule = new GroovyClassRule();
//  static Rule javaClassRule = new JavaClassRule();
//  static Rule javaDynamicClassRule = new JavaDynamicClassRule();

  static Map<String, Object> evaluateParameters = new HashMap<>();

  static {
    evaluateParameters.put("age", 20);
    evaluateParameters.put("vip", "ajksldfjoiefjaljlvipajsflijals");

    evaluateParameters.put("ali_score", 120);
    evaluateParameters.put("wx_score", 40);

    evaluateParameters.put("pd_living_province", "塔吉克斯坦");
    evaluateParameters.put("pd_census_province", "吉尔吉斯斯坦");
  }

  public DynamicScriptRuleBenchmark() {
    System.out.println("DynamicScriptRuleBenchmark 我被创建了");
  }

  @Benchmark
  public void testEmptyRule() {
    Preconditions.checkArgument(emptyRule.evaluate(evaluateParameters), "EmptyRule");
    Preconditions.checkArgument(emptyRule.execute(evaluateParameters), "EmptyRule");
  }

  @Benchmark
  public void testJavaRule() {
    Preconditions.checkArgument(javaRule.evaluate(evaluateParameters), "JavaRule");
    Preconditions.checkArgument(javaRule.execute(evaluateParameters), "JavaRule");
  }

  @Benchmark
  public void testJexlRule() {
    Preconditions.checkArgument(jexlRule.evaluate(evaluateParameters), "JexlRule");
    Preconditions.checkArgument(jexlRule.execute(evaluateParameters), "JexlRule");
  }

  @Benchmark
  public void testMvelRule() {
    Preconditions.checkArgument(mvelRule.evaluate(evaluateParameters), "MvelRule");
    Preconditions.checkArgument(mvelRule.execute(evaluateParameters), "MvelRule");
  }

  @Benchmark
  public void testSpELRule() {
    Preconditions.checkArgument(spELRule.evaluate(evaluateParameters), "SpELRule");
    Preconditions.checkArgument(spELRule.execute(evaluateParameters), "SpELRule");
  }

  @Benchmark
  public void testQLExpressRule() {
    Preconditions.checkArgument(qLExpressRule.evaluate(evaluateParameters), "QLExpressRule");
    Preconditions.checkArgument(qLExpressRule.execute(evaluateParameters), "QLExpressRule");
  }

  @Benchmark
  public void testGroovyScriptRule() {
    Preconditions.checkArgument(groovyScriptRule.evaluate(evaluateParameters), "GroovyScriptRule");
    Preconditions.checkArgument(groovyScriptRule.execute(evaluateParameters), "GroovyScriptRule");
  }

  @Benchmark
  public void testAviatorRule() {
    Preconditions.checkArgument(aviatorRule.evaluate(evaluateParameters), "AviatorRule");
    Preconditions.checkArgument(aviatorRule.execute(evaluateParameters), "AviatorRule");
  }

  @Benchmark
  public void testOGNLRule() {
    Preconditions.checkArgument(ognlRule.evaluate(evaluateParameters), "OGNLRule");
    Preconditions.checkArgument(ognlRule.execute(evaluateParameters), "OGNLRule");
  }

  @Benchmark
  public void testFelRule() {
    Preconditions.checkArgument(felRule.evaluate(evaluateParameters), "FelRule");
    Preconditions.checkArgument(felRule.execute(evaluateParameters), "FelRule");
  }

  @Benchmark
  public void testJSELRule() {
    Preconditions.checkArgument(jselRule.evaluate(evaluateParameters), "JSELRule");
    Preconditions.checkArgument(jselRule.execute(evaluateParameters), "JSELRule");
  }

  @Benchmark
  public void testGroovyClassRule() {
    Preconditions.checkArgument(groovyClassRule.evaluate(evaluateParameters), "GroovyClassRule");
    Preconditions.checkArgument(groovyClassRule.execute(evaluateParameters), "GroovyClassRule");
  }


//  @Benchmark
//  public void testJavaClassRule() {
//    Preconditions.checkArgument(javaClassRule.evaluate(evaluateParameters), "JavaClassRule");
//    Preconditions.checkArgument(javaClassRule.execute(evaluateParameters), "JavaClassRule");
//  }

  /**
   * JavaHHHRule.java:5: 警告: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
   */
//  @Benchmark
//  public void testJavaDynamicClassRule() {
//    Preconditions.checkArgument(javaDynamicClassRule.evaluate(evaluateParameters), "JavaDynamicClassRule");
//    Preconditions.checkArgument(javaDynamicClassRule.execute(evaluateParameters), "JavaDynamicClassRule");
//  }
  public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
        .include(DynamicScriptRuleBenchmark.class.getSimpleName())
        .forks(1)
        .build();

    new Runner(opt).run();
  }
}
